home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- *
- * Apple Developer Technical Support
- *
- * Menu handling routines
- *
- * Program: EditionSample
- * File: Menu.c - C Source
- *
- * by: C.K. Haun <TR>
- *
- * Copyright © 1990,1991 Apple Computer, Inc.
- * All rights reserved.
- *
- *------------------------------------------------------------------------------
- * This file handles menu stuff, swapping checkmarks, handling simple dialog
- * boxes, and the like. If it happens because of a menu selection, it comes through
- * here.
- *----------------------------------------------------------------------------*/
-
- #define __SAMPMENU__
-
- #pragma segment Main
- #pragma load "EdSampheaders" /* see the Buildheaders.c file */
-
- #include "EdSampdefines.h"
-
- /* prototypes */
- void StartMenus();
- void SetUndo(short undoNow, Boolean fromRecord);
- Boolean DoSelected(long val);
- void SetCurAction(short actionIn);
- void DoDaCall(MenuHandle themenu, long theit);
- void SwitchChecks(short itemNow);
- void SetMyCursor(short myCurs);
- void SetWMenus(Boolean how);
- OSErr PrepQuit(void);
- void AdjustMenus(windowCHandle tempCH);
- /* external references */
- extern Handle gMymenu;
- extern MenuHandle gAppleMenuHandle, gFileMenuHandle, gEditMenuHandle, gToolMenuHandle, gAdditionalMenu;
- extern Boolean gStop;
- extern void SaveMe(windowCHandle theWind, WindowPtr theWindPtr);
- extern void CloseClip(void);
- extern WindowPtr OpenFile(FSSpec *inSpec);
- extern void UndoLast(void);
- extern WindowPtr AddNewWindow(Boolean showIt);
- extern void DoOptions(SectionHandle inSection);
- extern void DeleteSubscriber(void);
- extern void PasteSubscription(void);
- extern void CutSubscription(void);
- extern void CopySubscription(void);
- extern void PrintIt(WindowPtr theWind, Boolean keepOpen);
- extern OSErr CreatePublisher(OSType typeToMake,Boolean fromEvent,AliasHandle theAlias);
- extern Boolean HasTESelection(windowCHandle inWind);
- extern Boolean PasteText(void);
- extern Boolean CutText(void);
- extern Boolean CopyText(void);
- extern Boolean ClearText(void);
-
- extern WindowPtr gActionWindows;
- extern WindowPtr gClipWindowPtr;
- short gWindowCount;
- extern Boolean gExpanded;
- extern Boolean gEdSamp;
- extern Boolean gShowingAll;
- Str255 undoString; /* for the current Undo menu item text */
- short pItemChecked;
- extern SectionHandle gShowingSecHandle;
- extern WindowPtr gCurrentWindow;
- /* file globals */
- CursHandle pCursHand = nil;
- extern Boolean gHasSelection;
- extern Boolean gShowPub;
- extern Boolean gShowSub;
- extern short gClipHasContents;
- /* StartMenus loads our menu bar resource, and initializes the menus
- * and some settings related to the menu items
- */
- /* and since it's an initializer, I'll put it in that segement */
- #pragma segment MyInit
- void StartMenus(void)
- {
- gMymenu = GetNewMBar(kOurMenuBar);
- SetMenuBar(gMymenu);
- DrawMenuBar();
- gAppleMenuHandle = GetMHandle(kAppleMenu);
- gFileMenuHandle = GetMHandle(kFileMenu);
- gEditMenuHandle = GetMHandle(kEditMenu);
- gToolMenuHandle = GetMHandle(kToolsMenu);
- gAdditionalMenu = GetMHandle(kAdditionalMenu);
- AddResMenu(gAppleMenuHandle, 'DRVR'); /* add DAs */
- }
-
- /* end StartMenus */
-
- #pragma segment Main
- /* DoSelected is called from the main event loop in response to a
- * MenuSelect or MenuKey action. Some events are handled here,
- * some just set options.
- */
-
- Boolean DoSelected(long val)
- {
- short loval, hival;
- Boolean temp = false;
- short tempActionOut;
- loval = LoWord(val);
- hival = HiWord(val);
- HiliteMenu(0);
- switch (hival) { /* switch off the menu number selected */
- case kAppleMenu: /* Apple menu */
- if (loval != 1) { /* if this was not About, it's a DA */
- DoDaCall(gAppleMenuHandle, loval);
- } else {
- Alert(128, nil); /* do about box */
- }
- break;
- case kFileMenu: /* File menu */
- switch (loval) {
- WindowPtr tempW;
- AliasHandle tempAlias; /* for SaveAs */
- windowCHandle tempName;
- case kNewItem:
- if (!(tempW = AddNewWindow(true))) /* Call AddNewWindow. If it fails, tell */
- Alert(kNoMoreWindows, nil); /* user why */
- else
- ChangePlane(tempW);
- break;
- case kOpenItem:
- OpenFile(nil); /* open a file, nil says that I am not passing a */
- /* FileSpec, promt the user for a name */
- break;
- case kCloseItem: /* call the close procedure for this window */
- (ProcPtr)((*(windowCHandle)((WindowPeek)gCurrentWindow)->refCon)->closeMe)(gCurrentWindow);
- break;
- case kSaveItem: /* call the save procedure for this window */
- HLock((Handle)(windowCHandle)((WindowPeek)gCurrentWindow)->refCon);
- (ProcPtr)(*
- ((windowCHandle)((WindowPeek)gCurrentWindow)->refCon))->
- saveMe((windowCHandle)((WindowPeek)gCurrentWindow)->refCon, gCurrentWindow);
- HUnlock((Handle)(windowCHandle)((WindowPeek)gCurrentWindow)->refCon);
- break;
- case kSaveAsItem: /* save under a different name */
- HLock((Handle)(windowCHandle)((WindowPeek)gCurrentWindow)->refCon);
- tempName = (windowCHandle)GetWRefCon(gCurrentWindow);
- /* save off the old alias in case the person cancels */
- tempAlias = (*tempName)->fileAliasHandle;
- /* get an empty handle and store it in the alias place, this tells the Save */
- /* function that this file has not yet been saved, so put up a SF box */
- (*tempName)->fileAliasHandle = (AliasHandle)NewHandle(0);
- (ProcPtr)(*
- ((windowCHandle)((WindowPeek)gCurrentWindow)->refCon))->
- saveMe((windowCHandle)((WindowPeek)gCurrentWindow)->refCon, gCurrentWindow);
- /* see if they really saved (by checking the size of the alias) and if they have */
- /* get rid of the alias you saved */
- if (GetHandleSize((Handle)(*tempName)->fileAliasHandle) == nil) {
- DisposHandle((Handle)(*tempName)->fileAliasHandle);
- (*tempName)->fileAliasHandle = tempAlias;
- } else {
- DisposHandle((Handle)tempAlias);
- }
- HUnlock((Handle)(windowCHandle)((WindowPeek)gCurrentWindow)->refCon);
- break;
-
- case kPrintItem:
- PrintIt(gCurrentWindow, true);
- break;
- case kQuitItem:
- PrepQuit(); /* Call PrepQuit, which sets our quit flag, so we */
- /* quit the next time through the event loop. The */
- /* AEQuit handler also calls this routine */
- break;
- }
- break;
- case kEditMenu:
- /* edit menu junk */
- switch (loval) {
- case kUndoItem:
- UndoLast(); /* Undoes the last action in the frontmost window */
- break;
- /* all the C/C/P stuff acts _first_ on any active TextEdit records. */
- /* if there aren't any, then it goes to other stuff */
- case kCutItem:
- if (!CutText())
- CutSubscription();
- break;
- case kCopyItem:
- if (!CopyText())
- CopySubscription();
- break;
- case kPasteItem:
- /* pasting text has precidence over subscriptions. Just because I felt like it */
- /* vary to suit the needs of your users and application */
- if (!PasteText())
- PasteSubscription();
- break;
- case kClearItem:
- /* Clear item acts as 'cancel' for the selected section */
- if (!ClearText())
- DeleteSubscriber();
- break;
- case kPublishItem:
- /* see if there is any text */
- if (HasTESelection((windowCHandle)GetWRefCon(FrontWindow())))
- CreatePublisher('TEXT',false,nil);
- else
- CreatePublisher('PICT',false,nil); /* in Publish.c */
- break;
- case kSubscribeItem:
- DoSubscribe(); /* in Subscribe.c */
- break;
- case kSoptionsItem:
- DoOptions(gShowingSecHandle); /* in Subscribe.c. Same */
- /* function is called for a publish or subscribe section */
- break;
- case kBorders:
- if (gShowingAll) {
- gShowingAll = false;
- SetItem(gEditMenuHandle, kBorders, "\pShow Borders");
- } else {
- gShowingAll = true;
- SetItem(gEditMenuHandle, kBorders, "\pHide Borders");
- }
- /* make sure they get redrawn */
- InvalRect(&(gCurrentWindow->portRect));
-
- break;
- case kClapNum:
- if (((WindowPeek)gClipWindowPtr)->visible) {
- CloseClip();
- } else {
- ShowWindow(gClipWindowPtr);
- ChangePlane(gClipWindowPtr);
- SetItem(gEditMenuHandle, kClapNum, "\pHide Clipboard");
-
- }
- break;
- /* TEST TEMP ••••• */
- case kClapNum + 1:
- /* TEST TEMP highlight the selection please */
- do {
- windowCHandle temp = (windowCHandle)GetWRefCon(FrontWindow());
- TEHandle theTEHandle = (*temp)->boxHandle;
- textSectionHandle currentSection = (*temp)->textSections;
- TESetSelect((*currentSection)->startChar, (*currentSection)->endChar, theTEHandle);
- } while (false);
- temp = true;
- break;
- default:
- Alert(132, nil); /* TEST TEMP */
- break;
-
- } /* edit menu switch */
- break;
- case kToolsMenu:
- switch (loval) {
- /* Clean this up, this is silly, I say, this is silly son..... */
- case kLineItem:
- tempActionOut = kDrawLine;
- break;
- case kRectItem:
- tempActionOut = kDrawRect;
- break;
- case kOvalItem:
- tempActionOut = kDrawOval;
- break;
- case kTextBoxItem:
- tempActionOut = kTextBox;
- break;
- case kSelectItem:
- tempActionOut = kSelectStuff;
- break;
- }
- /* these set the current action for the front window, set the */
- /* cursor for the selected tool, and check the correct menu item */
- SetCurAction(tempActionOut);
- SetMyCursor(loval);
- SwitchChecks(loval);
- break;
- case kAdditionalMenu:
- switch (loval) {
- EditionInfoRecord tempRecord;
- Str255 orgDate;
- Str255 modDate;
- char parm1[5];
- char parm2[5];
- case kExpandedItem:
- if (gExpanded)
- gExpanded = false;
- else
- gExpanded = true;
- CheckItem(gAdditionalMenu, kExpandedItem, gExpanded);
-
- break;
- case kGetSecInfo:
- GetEditionInfo(gShowingSecHandle, &tempRecord);
- IUDateString(tempRecord.crDate, longDate, orgDate);
- IUDateString(tempRecord.mdDate, longDate, modDate);
- parm1[0] = 4;
- parm2[0] = 4;
- BlockMove((Ptr)&tempRecord.fdCreator, (Ptr)&parm1[1], 4);
- BlockMove((Ptr)&tempRecord.fdType, (Ptr)&parm2[1], 4);
- ParamText(orgDate, modDate, parm1, parm2);
- Alert(kEdInfo, nil);
- break;
- case kEdSampFiles:
- if (gEdSamp)
- gEdSamp = false;
- else
- gEdSamp = true;
- CheckItem(gAdditionalMenu, kEdSampFiles, gEdSamp);
- break;
- }
- break;
- }
- return(temp);
- }
-
- /* end DoSelected */
-
- /* DoDaCall opens the requested DA. It's here as a seperate routine if you'd */
- /* like to perform some action or just know when a DA is opened in your */
- /* layer. Can be handy to track memory problems when a DA is opened */
- /* with an Option-open */
- void DoDaCall(MenuHandle themenu, long theit)
- {
- long qq;
- char DAname[255];
- GetItem(themenu, theit, &DAname);
- qq = OpenDeskAcc(DAname);
- }
-
- /* end DoDaCall */
-
-
- /* PrepQuit sets the stop flag. Called from the menu or from the */
- /* quit AppleEvent handler. It also calls the close function on all open windows, and */
- /* cancels the quit if a cancel happened */
- /* Also unregisters the ClipBoard section, if one exists */
- OSErr PrepQuit(void)
- {
- gStop = true;
- while (gWindowCount) {
- CloseMyWindow(gActionWindows); /* step through until all are closed */
- if (gStop == false)
- break; /* if this is false, they hit cancel in the close routine */
- }
- if (gStop) {
- extern short gClipHasContents;
- extern SectionHandle gClipSection;
- /* clear the clipboard */
- if (gClipHasContents == kClipHasSub) {
- UnRegisterSection(gClipSection);
- DisposHandle((Handle)(*gClipSection)->alias);
- DisposeHandle((Handle)gClipSection);
- gClipSection = nil;
- gClipHasContents = kClipEmpty;
- }
- return(noErr); /* quit will happen */
- } else {
- return(userCanceledErr);
- } /* user stopped the termination */
- }
-
- /* end PrepQuit */
-
- /* SetUndo sets the text of the Undo item based on the last action in */
- /* the current window, if fromRecord is true, else use*/
- /* undoNow */
- void SetUndo(short undoNow, Boolean fromRecord)
- {
- windowCHandle temp;
- if (gCurrentWindow == nil) {
- GetIndString(undoString, kUndoStringsRes, 1);
- SetItem(gEditMenuHandle, kUndoItem, &undoString);
- DisableItem(gEditMenuHandle, kUndoItem);
- } else {
- temp = (windowCHandle)GetWRefCon(gCurrentWindow);
- if (fromRecord)
- undoNow = (*temp)->undoAction;
- else
- (*temp)->undoAction = undoNow;
- GetIndString(undoString, kUndoStringsRes, undoNow + 1);
- SetItem(gEditMenuHandle, kUndoItem, &undoString);
- if (undoNow)
- EnableItem(gEditMenuHandle, kUndoItem);
- else
- DisableItem(gEditMenuHandle, kUndoItem);
- }
- }
-
- /* end SetUndo */
-
- /* SetMyCursor sets the cursor to the correct shape for the */
- /* tool in use */
- void SetMyCursor(short myCurs)
- {
- if (pCursHand != nil) {
- ReleaseResource((Handle)pCursHand);
- pCursHand = nil;
- }
- if (myCurs == 0) {
- InitCursor();
- } else {
- switch (myCurs) {
- case kLineItem:
- pCursHand = GetCursor(crossCursor);
- break;
- case kRectItem:
- case kTextBox:
- case kOvalItem:
- pCursHand = (CursHandle)GetResource('CURS', 126 + myCurs);
- break;
- case kSelectItem:
- pCursHand = (CursHandle)GetResource('CURS', 131);
- break;
- }
- SetCursor(*pCursHand);
- }
- }
-
- /* end SetMyCursor */
-
- /* SwitchChecks sets the checking of the tool menu items */
- void SwitchChecks(short itemNow)
- {
- Boolean how;
- if (pItemChecked == itemNow) { /* is this item currently checked? If so */
- how = false; /* uncheck it and set action to nil */
- SetCurAction(kNoAction);
- InitCursor();
- pItemChecked = 0;
- } else {
- CheckItem(gToolMenuHandle, pItemChecked, false);
- pItemChecked = itemNow;
- how = true;
- }
- CheckItem(gToolMenuHandle, itemNow, how);
- }
-
- /* end SwitchChecks */
-
- /* SetCurAction sets the action value in the window record */
- void SetCurAction(short actionIn)
- {
- if (gCurrentWindow != nil) {
- windowCHandle shortname = (windowCHandle)GetWRefCon(gCurrentWindow);
- HLock((Handle)shortname);
- (*shortname)->currentAction = actionIn;
- HUnlock((Handle)shortname);
- }
- }
-
- /* end SetCurAction */
-
- void SetWMenus(Boolean how)
- {
- register qq;
- if (how) {
- EnableItem(gFileMenuHandle, kCloseItem);
- EnableItem(gFileMenuHandle, kPrintItem);
- EnableItem(gFileMenuHandle, kSaveItem);
- EnableItem(gFileMenuHandle, kSaveAsItem);
- EnableItem(gEditMenuHandle, kSubscribeItem);
- EnableItem(gToolMenuHandle, 0);
- for (qq = 1; qq < 5; qq++) {
- EnableItem(gToolMenuHandle, qq);
- }
- } else {
- DisableItem(gFileMenuHandle, kCloseItem);
- DisableItem(gFileMenuHandle, kPrintItem);
- DisableItem(gFileMenuHandle, kSaveItem);
- DisableItem(gFileMenuHandle, kSaveAsItem);
- DisableItem(gEditMenuHandle, kSubscribeItem);
- DisableItem(gToolMenuHandle, 0); /* kill this whole thing */
- }
- DrawMenuBar();
- }
-
- /* end SetWMenus */
-
- void AdjustMenus(windowCHandle tempCH)
- {
- register qq;
- /* Do we currently */
- /* have an area selected? */
- if (gHasSelection || HasTESelection(tempCH)) /* Yes, enable the Publish item */
- EnableItem(gEditMenuHandle, kPublishItem);
- else /* No, dim the publish item */
- DisableItem(gEditMenuHandle, kPublishItem);
- /* Next, are we showing a border around a subscriber or */
- /* publisher? If so, then we allow the user to bring up */
- /* the Options dialog */
- if (gShowPub || gShowSub) {
- EnableItem(gEditMenuHandle, kSoptionsItem);
- EnableItem(gAdditionalMenu, kGetSecInfo);
- } else {
- DisableItem(gEditMenuHandle, kSoptionsItem);
- DisableItem(gAdditionalMenu, kGetSecInfo);
- }
- if (!(FrontWindow() == gClipWindowPtr) && (gCurrentWindow != nil)) { /* is there a document window in front? */
- EnableItem(gFileMenuHandle, kSaveItem);
- if (GetHandleSize((Handle)(*tempCH)->fileAliasHandle) == nil)
- SetItem(gFileMenuHandle, kSaveItem, "\pSave...");
- else
- SetItem(gFileMenuHandle, kSaveItem, "\pSave");
- /* I'm only allowing a text box if there are no other objects. */
- if ((*tempCH)->boxHandle != nil || (*tempCH)->ovalCount != 0 || (*tempCH)->rectCount != 0 || (*tempCH)->lineCount != 0)
- DisableItem(gToolMenuHandle, kTextBoxItem);
- else
- EnableItem(gToolMenuHandle, kTextBoxItem);
-
- } else {
- DisableItem(gFileMenuHandle, kSaveItem);
- }
- /* if there is a subscriber, or a subscription on the clipboard, or a TEselection enable the edit commands */
- if (gShowSub || HasTESelection(tempCH)) {
- EnableItem(gEditMenuHandle, kCutItem);
- EnableItem(gEditMenuHandle, kCopyItem);
- EnableItem(gEditMenuHandle, kClearItem);
- } else {
- DisableItem(gEditMenuHandle, kCutItem);
- DisableItem(gEditMenuHandle, kCopyItem);
- DisableItem(gEditMenuHandle, kClearItem);
- }
- if (gClipHasContents == kClipHasSub || (gClipHasContents == kClipHasText && (*tempCH)->boxHandle != nil) && (tempCH != nil)) {
- EnableItem(gEditMenuHandle, kPasteItem);
- } else {
- DisableItem(gEditMenuHandle, kPasteItem);
- }
- }
-
-
- #undef __SAMPMENU__
-